function [CompChoice, CompStrat, RightProb] = GetCompChoice_b3_nocolor(Hinfo,Cinfo)
% old function [CompChoice, CompStrat, RightProb] = GetCompChoice_b3_nocolor(Hinfo,Cinfo)
% [CompChoice, CompStrat, RightProb] = GetCompChoice_b3_nocolor(Hinfo,Cinfo)
%
% CompChoice = 0=left; 1=right;
% CompStrat = why;  strat_text={'random','left','right','green','red'};
% RightProb = 0:1,  0=abs left; 1=abs right; strength of choice
%
% HInfo= 2 column array of past human behavior [leftVright, redVgreen];
% CInfo= 2 column array of past computer behavior [leftVright, redVgreen];
% green_on_right = is the green tgt on the right hand side for the current trial?
%
% bcoe redid this 

    RightProb=[.5 .5];
CompChoice=RightProb;
if nargin==0
        Hinfo=[1 0 1 0 1 0 1 0; 1 0 0 1 1 0 0 1];
    Cinfo=round(rand(size(Hinfo)));
    green_on_right=0; %#ok<NASGU>

%     help(mfilename)
%     return
end
strat_text={'left','right','red','green','guess'};

trialnum=length(Hinfo);
if trialnum <6
    CompChoice=round(rand); % left or right
    CompStrat= strat_text{5}; % left right green red
    RightProb=[.5 .5];
    return
end


n= rand;%/2+.25; %  50% swing, centered 
for jj=1%:2 % first check leftVright (0,1) then check redVgreen (0,1)
    Flagit=zeros(1,8);
    Prob=zeros(1,8);
    pvalue=ones(1,8);
    RightProb(jj) = 99;
    for i = 1:4, %
        if trialnum > i+1
            [Flagit(i),Prob(i),pvalue(i)]=checkPattern(trialnum,i,Hinfo(:,jj));
        end
    end
    for i = 1:4,
        if trialnum > i+1
            [Flagit(i+4),Prob(i+4),pvalue(i+4)]=checkOutcome(trialnum,i,Hinfo(:,jj),Cinfo(:,jj));
        end
    end       
    %[Flagit(9),Prob(9),pvalue(9)]=check0Pattern(trialnum,Hinfo(:,jj));
    [Flagit(9),Prob(9),pvalue(9)]=check0Pattern(Hinfo(:,jj)); %#ok<NASGU>
    
    Deviation = zeros(1,length(Flagit));
    
    if sum(Flagit) > 0
        %             for i=1:9,
        %                 if Flagit(i)==1
        %                     Deviation(i)= abs(0.5-Prob(i));
        %                 end
        %             end
        Deviation(Flagit==1)=abs(0.5-Prob(Flagit==1));
        %maxIndex=findstr(max(Deviation), Deviation);
        [val, loc]=max(Deviation); %#ok<ASGLU>
        RightProb(jj)= Prob(loc);
        %keyboard
        if n < RightProb(jj)
            CompChoice(jj) = 1;
        else
            CompChoice(jj) = 0;
        end
    else
        RightProb(jj)=.5+rand/100000; % avoid ties for the final decision
        CompChoice(jj) = -1; %   
    end
end
if length(Hinfo)>500
    fred;
end
%if abs(.5-RightProb(1))>abs(.5-RightProb(2))% direction or color?
    temp=[5 1 2 ];
    CompStrat=strat_text(temp(CompChoice(1)+2));
    CompChoice=CompChoice(1);
    %RightProb=RightProb(1);
% else
%     temp=[5 3 4 ];
%     CompStrat=strat_text(temp(CompChoice(2)+2));
%     CompChoice=CompChoice(2)==green_on_right;
%     %    RightProb=RightProb(2);
% end
clear temp
if CompChoice<0
    CompChoice=round(rand);
end
%0=left;
%1=right;
%2=green;
%3=red;
%4=random;

return
%--------------------------------------------------------------------------
function [flag,prob,pvalue]= checkPattern(trialnum,pattern,Hinfo)
%global Hinfo;
Hinfo=Hinfo(:)';
flag = 0;
prob = 0;
pvalue = 1;
Last = Hinfo(trialnum-pattern + 1:trialnum);
NextTrials = findstr(Hinfo,Last) + pattern;
%NextTrials = NextTrials(1:length(NextTrials)-1);
NextTrials(end)=[];
NextChoices = Hinfo(NextTrials);
if ~isempty(NextChoices)
    pvalue = binotest2(sum(NextChoices),length(NextChoices)); 
    flag = pvalue < 0.05;
    prob = sum(NextChoices)/length(NextChoices);
end
return
%--------------------------------------------------------------------------
%function [flag,prob,pvalue] = check0Pattern(trialnum,Hinfo);
function [flag,prob,pvalue] = check0Pattern(Hinfo)
%global Hinfo;
pvalue = binotest2(sum(Hinfo), length(Hinfo));
flag = pvalue < 0.05;
prob = sum(Hinfo)/length(Hinfo);
return
%--------------------------------------------------------------------------
function [flag,prob,pvalue]= checkOutcome(trialnum,pattern,Hinfo,Cinfo)
%global Hinfo;
%global Cinfo;
Hinfo=Hinfo(:)';
Cinfo=Cinfo(:)';
flag = 0;
prob = 0;
pvalue = 1;
Last = Cinfo(trialnum-pattern + 1:trialnum);
NextTrials = findstr(Cinfo,Last) + pattern;
%NextTrials = find(Cinfo==Last) + pattern;
NextTrials(end)=[];% = NextTrials(1:length(NextTrials)-1);
NextChoices = Hinfo(NextTrials);
if ~isempty(NextChoices)
    pvalue = binotest2(sum(NextChoices),length(NextChoices)); 
    flag = pvalue < 0.05;
    prob = sum(NextChoices)/length(NextChoices);
end
return

%--------------------------------------------------------------------------
% function storeHumanInfo(Human, trialnum,Hinfo)
% global Hinfo;
% Hinfo(trialnum)= Human;
% return
% %--------------------------------------------------------------------------
% function storeCompInfo(Comp, trialnum,Cinfo)
% global Cinfo;
% Cinfo(trialnum)= Comp;
% return
%--------------------------------------------------------------------------
